
package ch.hearc.meteo.imp.afficheur.real.vue;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import ch.hearc.meteo.imp.afficheur.real.moo.StationMeteoElement;
import ch.hearc.meteo.imp.afficheur.real.vue.atome.JPanelStationMeteoListElement;
import ch.hearc.meteo.tools.ImageShop;

public class JPanelListeStationMeteo extends JPanel
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public JPanelListeStationMeteo()
		{
		listeStationMeteo = new ArrayList<JPanelStationMeteoListElement>();
		geometry();
		control();
		apparence();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/
	public void addStationMeteo(JPanelStationMeteoListElement panelStationMeteoListElement)
		{
		this.listeStationMeteo.add(panelStationMeteoListElement);
		System.out.println("Station added");
		this.remove(this.getComponentCount() - 1);
		panelStationMeteoListElement.setAlignmentX(Component.LEFT_ALIGNMENT);
		this.add(panelStationMeteoListElement);
		this.add(Box.createVerticalGlue());

		}

	public void removeStationMeteo(JPanelStationMeteoListElement panelStationMeteoListElement)
		{
		this.listeStationMeteo.remove(panelStationMeteoListElement);
		this.remove(panelStationMeteoListElement);
		validate();
		repaint();
		}

	public void removeAllStationsMeteo()
		{
		ArrayList<StationMeteoElement> listeStationsMeteoToDel = new ArrayList<StationMeteoElement>();
		for(JPanelStationMeteoListElement panelStationMeteoElement:this.listeStationMeteo)
			{

			listeStationsMeteoToDel.add(panelStationMeteoElement.getStationMeteoElement());
			}
		for(StationMeteoElement stationMeteoElement:listeStationsMeteoToDel)
			{
			stationMeteoElement.getPanelStationMeteoListElement().setVisible(false);
			try
				{
				stationMeteoElement.getAfficheurService().disconnect();
				stationMeteoElement.getPcLocal().getAfficheurService().disconnect();
				stationMeteoElement.getPcLocal().getMeteoService().disconnect();
				}
			catch (Exception e)
				{

				}
			}
		}

	public void applyMeteoStationFilter(String filter)
		{
		synchronized (this.listeStationMeteo)
			{

			for(JPanelStationMeteoListElement panelStationMeteoListElement:this.listeStationMeteo)
				{
				if (filter.equals(""))
					{
					panelStationMeteoListElement.setVisible(true);
					}
				else
					{
					panelStationMeteoListElement.setVisible(panelStationMeteoListElement.getStationMeteoElement().getAffichageOption().getTitre().toLowerCase().contains(filter.toLowerCase()));
					}
				}
			}
		validate();
		repaint();
		}


	@Override
	protected void paintComponent(Graphics g)
		{
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		Image logo = ImageShop.LOGO_HE_ARC;
		int y = this.getHeight() - logo.getHeight(this);
		g2d.drawImage(logo, 10, y-10, this);
		}

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/
	@Override
	public void setEnabled(boolean b)
		{
		super.setEnabled(b);
		for(Component c:this.getComponents())
			{
			c.setEnabled(b);
			}
		}

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	private void geometry()
		{
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(Box.createVerticalGlue());
		}

	private void control()
		{

		}

	private void apparence()
		{

		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	private ArrayList<JPanelStationMeteoListElement> listeStationMeteo;
	}
